﻿using Abp.UI;
using Mt.Site.Core.WorkFlows.Enum;
using Mt.Site.Core.WorkFlows.Process;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Core.WorkFlows.Diy.Actions.Impl
{
    /// <summary>
    /// 主动撤回
    /// </summary>
    public class WithdrawAction : ActionBase
    {
        public override string UniqueName => WorkFlowConst.OperationName.WithdrawActionName;

        private readonly IWorkFlowRollBackService _workFlowRollBackService;

        public WithdrawAction(IWorkFlowRollBackService workFlowRollBackService)
        {
            _workFlowRollBackService = workFlowRollBackService;
        }

        /// <summary>
        /// 是否显示该操作按钮
        /// 如果当前状态是在办，并且上一个经手人是当前用户，并且待办人员还没有查看过新稿件
        /// </summary>
        /// <param name="auditing"></param>
        /// <returns></returns>
        public override async Task<bool> IsShowAsync(IWorkFlowManager manager)
        {
            return await _workFlowRollBackService.CheckWithdrawBtnState(manager);
        }

        /// <summary>
        /// 操作处理
        /// </summary>
        /// <param name="auditing"></param>
        /// <param name="data"></param>
        public override async Task OperateAsync(IWorkFlowManager manager, object data)
        {
            var cleanMsg = manager.WorkFlowPluginFunction.CleanMsgWhenDrawback(manager);
            var taskId = await _workFlowRollBackService.WithdrawAsync(manager, cleanMsg);
            if (taskId != Guid.Empty)
            {
                return;
            }

            throw new UserFriendlyException("稿件已被查看或不是最后操作人，无法执行撤回操作！");
        }
    }
}
